package com.yongninger.activity.customer;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.yongninger.activity.R;
import com.yongninger.dao.CustomerInfoDao;
import com.yongninger.dao.ShipmentsDao;
import com.yongninger.vo.ClerkInfo;
import com.yongninger.vo.CustomerInfo;

public class ViewActivity extends Activity {
	private CustomerInfoDao customerInfoDao;
	private ShipmentsDao shipmentsDao;
	private String[] arrClerk;
	CustomerInfo customerInfo;
	EditText cccname;
	EditText cccode;
	EditText cccontact;
	EditText cccaddress;
	EditText cccperson;
	Spinner ccclerk;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customer_view);
		customerInfoDao = new CustomerInfoDao(getApplicationContext());
		Intent intent = getIntent();
		customerInfo = (CustomerInfo) intent
				.getSerializableExtra("customerInfo");
		cccname = (EditText)findViewById(R.id.cccname);
		cccname.setText(customerInfo.getCname());
		cccode = (EditText)findViewById(R.id.cccode);
		cccode.setText(customerInfo.getCcode());
		cccontact = (EditText)findViewById(R.id.cccontact);
		cccontact.setText(customerInfo.getCcontact());
		cccaddress = (EditText)findViewById(R.id.cccaddress);
		cccaddress.setText(customerInfo.getCaddress());
		cccperson = (EditText)findViewById(R.id.cccperson);
		cccperson.setText(customerInfo.getCperson());
		shipmentsDao = new ShipmentsDao(getApplicationContext());
		List<ClerkInfo> listClerk = shipmentsDao.queryClerkInfoVo();
		arrClerk = new String[listClerk.size()];
		int position = 0;
		for (int i = 0; i < listClerk.size(); i++) {
			if (listClerk.get(i).getCname().equals(customerInfo.getCclerk())) {
				position = i;
			}
			arrClerk[i] = listClerk.get(i).getCname();
		}
		ccclerk = (Spinner) findViewById(R.id.ccclerk);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arrClerk);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		ccclerk.setAdapter(adapter);
		ccclerk.setVisibility(View.VISIBLE);
		ccclerk.setSelection(position);

		Button modifyBtn = (Button) this.findViewById(R.id.modify);
		OnClickListener onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (update()) {
					Toast.makeText(getApplicationContext(), "修改成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "修改失败", 2).show();
				}
			}
		};
		modifyBtn.setOnClickListener(onClickListener);

		/*
		 * 删除按钮
		 */
		Button deleteBtn = (Button) this.findViewById(R.id.delete);
		OnClickListener onClickListener1 = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 删除
				boolean result = customerInfoDao.deleteCustomerVo(customerInfo.getCid());
				if (result) {
					Toast.makeText(getApplicationContext(), "删除成功", 2).show();
					// 跳转到查询页面
					Intent intent = new Intent(ViewActivity.this,
							ListActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "删除失败", 2).show();
				}
			}
		};
		deleteBtn.setOnClickListener(onClickListener1);
	}

	private boolean update() {
		CustomerInfo customerInfoTemp = new CustomerInfo();
		customerInfoTemp.setCcode(cccode.getText().toString());
		customerInfoTemp.setCname(cccname.getText().toString());
		customerInfoTemp.setCaddress(cccaddress.getText().toString());
		customerInfoTemp.setCclerk(ccclerk.getSelectedItem().toString());
		customerInfoTemp.setCcontact(cccontact.getText().toString());
		customerInfoTemp.setCperson(cccperson.getText().toString());
		return customerInfoDao.updateCustomerVo(customerInfoTemp, customerInfo.getCid());
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Toast.makeText(getApplicationContext(), "请先保存内容",
					Toast.LENGTH_SHORT).show();
			return false;
		}else if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() >= 0){
			finish();
			// 跳转到查询页面
			Intent intent = new Intent(ViewActivity.this,
					ListActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
